﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;
using System.Security.Cryptography;

public partial class ForgotPassword : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.FindControl("LogoutButton").Visible = false;
    }

    static string getMd5Hash(string input)
    {
        // Create a new instance of the MD5CryptoServiceProvider object.
        MD5 md5Hasher = MD5.Create();

        // Convert the input string to a byte array and compute the hash.
        byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

        // Create a new Stringbuilder to collect the bytes
        // and create a string.
        StringBuilder sBuilder = new StringBuilder();

        // Loop through each byte of the hashed data 
        // and format each one as a hexadecimal string.
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }

        // Return the hexadecimal string.
        return sBuilder.ToString();
    }

    public static string RandomNumbersMethod(int length)
    {
        string result = string.Empty;

        Random random = new Random();
        for (int i = 0; i < length; i++)
        {
            char ch = Convert.ToChar(random.Next(97, 122));
            result += ch;
        }

        return result;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if(TextBox1.Text != "admin")
        {
            Response.Write("<Script language=javascript>alert('Enter correct username.')</Script>");
            return;
        }
        try
        {
            DbUtilities db = new DbUtilities();
            String sql = "SELECT * FROM admin";
            DbUtilities dbUtil = new DbUtilities(sql);
            DataSet dataSet = dbUtil.retrieveInfo();
            String password = (String)dataSet.Tables[0].Rows[0].ItemArray[1];
            string emailAddress = (String)dataSet.Tables[0].Rows[0].ItemArray[0];

            string newPassword = RandomNumbersMethod(10);

            string newPassword_Hash = getMd5Hash(newPassword);
            db.changePassword(password, newPassword_Hash);
            Mailer mailer = new Mailer();
            mailer.mailNewPassword(emailAddress, newPassword);
        }

        catch (Exception ex)
        {
            Response.Write("<Script language=javascript>alert('Some Error Occured!!')</Script>");
        }

        Response.Write("<Script language=javascript>alert('New Password is sent to your email.')</Script>");
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
}
